package com.youi.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youi.common.SessionKey;
import com.youi.dao.MerchantwithdrawdetailDao;
import com.youi.entity.Merchantwithdraw;
import com.youi.entity.Merchantwithdrawdetail;
import com.youi.entity.Sysuser;
import com.youi.entity.Userorderexcel;
import com.youi.manage.HttpSessionManage;
import com.youi.service.MerchantwithdrawService;
import com.youi.service.MerchantwithdrawdetailService;
import com.youi.service.UserorderexcelService;
import com.youi.third.ali.oss.OSSUtil;
import com.youi.util.DateUtil;
import com.youi.util.FileUploadUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.SmsUtil;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

@Service
public class MerchantwithdrawdetailServiceImpl extends BaseServiceImpl<Merchantwithdrawdetail> implements MerchantwithdrawdetailService{
		
		private MerchantwithdrawdetailDao merchantwithdrawdetailDao;
		
		@Autowired
		private MerchantwithdrawService merchantwithdrawService;
		@Autowired
		private UserorderexcelService userorderexcelService;
		@Autowired
		public void setfaultRecordDao(MerchantwithdrawdetailDao merchantwithdrawdetailDao){
			this.merchantwithdrawdetailDao=merchantwithdrawdetailDao;
			super.setBaseDAO(merchantwithdrawdetailDao);
		}

		@Override
		public void getMerchantwithdrawdetailMsg(Merchantwithdrawdetail merchantwithdrawdetail) {
			// TODO Auto-generated method stub
			if (merchantwithdrawdetail!=null) {
				Merchantwithdraw merchantwithdraw=merchantwithdrawService.get(merchantwithdrawdetail.getMerchantwithdrawId());
				if (merchantwithdraw!=null) {
					merchantwithdrawdetail.setMerchantwithdraw(merchantwithdraw);
				}
			}
		}

		@Override
		public void getMerchantwithdrawdetailMsg(List<Merchantwithdrawdetail> merchantwithdrawdetailList) {
			// TODO Auto-generated method stub
			if (merchantwithdrawdetailList!=null&&merchantwithdrawdetailList.size()>0) {
				for (Merchantwithdrawdetail merchantwithdrawdetail : merchantwithdrawdetailList) {
					getMerchantwithdrawdetailMsg(merchantwithdrawdetail);
				}
			}
		}

		@Override
		public void toExcelMerchantwithdraw(HttpServletRequest request,List<Merchantwithdrawdetail> merchantwithdrawdetailList) {
			// TODO Auto-generated method stub			
			try {
				Sysuser sysuser=(Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
				String fileName="merchantwithdraw"+new Date().getTime()+""+SmsUtil.getRandom(3)+".xls";//文件名				
				String fileUrl=FileUploadUtil.getUploadUrl(request, "filedownload"+File.separator+fileName);//生成路径
				WritableWorkbook workbook = Workbook.createWorkbook(new File(fileUrl));
				WritableSheet sheet1=workbook.createSheet("Sheet1", 0);
				WritableFont font=new WritableFont(WritableFont.createFont("微软雅黑"),10);
				WritableCellFormat format=new WritableCellFormat();
				format.setAlignment(Alignment.CENTRE);
				format.setVerticalAlignment(VerticalAlignment.CENTRE);
				Label label=new Label(0, 0, "订单号", format);
				sheet1.addCell(label);
				label=new Label(1, 0, "提现单号", format);
				sheet1.addCell(label);
				label=new Label(2, 0, "期号", format);
				sheet1.addCell(label);
				label=new Label(3, 0, "供应商名称", format);
				sheet1.addCell(label);
				label=new Label(4, 0, "供应商编码", format);
				sheet1.addCell(label);
				label=new Label(5, 0, "商品名称", format);
				sheet1.addCell(label);
				label=new Label(6, 0, "单价", format);
				sheet1.addCell(label);
				label=new Label(7, 0, "数量", format);
				sheet1.addCell(label);
				label=new Label(8, 0, "订单金额", format);
				sheet1.addCell(label);
				label=new Label(9, 0, "分润金额", format);
				sheet1.addCell(label);
				label=new Label(10, 0, "分润比例", format);
				sheet1.addCell(label);
				label=new Label(11, 0, "下单时间", format);
				sheet1.addCell(label);
				label=new Label(12, 0, "订单状态", format);
				sheet1.addCell(label);
				label=new Label(13, 0, "分润状态", format);
				sheet1.addCell(label);
				label=new Label(14, 0, "订单完成时间", format);
				sheet1.addCell(label);
				label=new Label(15, 0, "提现方式", format);
				sheet1.addCell(label);
				label=new Label(16, 0, "提现时间", format);
				sheet1.addCell(label);
				Merchantwithdrawdetail merchantwithdrawdetail=null;
				for (int i = 0,j=1; i < merchantwithdrawdetailList.size(); i++,j++) {
					merchantwithdrawdetail=merchantwithdrawdetailList.get(i);
					ObjectUtil.ToChangeNullToEmpty(merchantwithdrawdetail);
					label=new Label(0, j, merchantwithdrawdetail.getOrderNo(), format);
					sheet1.addCell(label);
					label=new Label(1, j, merchantwithdrawdetail.getWithdrawNo(), format);
					sheet1.addCell(label);
					label=new Label(2, j, merchantwithdrawdetail.getDateNo(), format);
					sheet1.addCell(label);
					label=new Label(3, j, merchantwithdrawdetail.getMerchantwithdraw().getMerchantName(), format);
					sheet1.addCell(label);
					label=new Label(4, j, merchantwithdrawdetail.getMerchantwithdraw().getMerchantCode(), format);
					sheet1.addCell(label);
					label=new Label(5, j, merchantwithdrawdetail.getGoodsName(), format);
					sheet1.addCell(label);
					if (merchantwithdrawdetail.getGoodsPrice()!=null) {
						label=new Label(6, j, String.valueOf(merchantwithdrawdetail.getGoodsPrice()), format);
						sheet1.addCell(label);
					}
					if (merchantwithdrawdetail.getGoodsCount()!=null) {
						label=new Label(7, j, String.valueOf(merchantwithdrawdetail.getGoodsCount()), format);
						sheet1.addCell(label);
					}
					if (merchantwithdrawdetail.getOrderAmount()!=null) {
						label=new Label(8, j, String.valueOf(merchantwithdrawdetail.getOrderAmount()), format);
						sheet1.addCell(label);
					}
					if (merchantwithdrawdetail.getMerchantAmount()!=null) {
						label=new Label(9, j, String.valueOf(merchantwithdrawdetail.getMerchantAmount()), format);
						sheet1.addCell(label);
					}
					if (merchantwithdrawdetail.getMerchanRatio()!=null) {
						label=new Label(10, j, String.valueOf(merchantwithdrawdetail.getMerchanRatio()), format);
						sheet1.addCell(label);
					}
					if (merchantwithdrawdetail.getGmtOrder()!=null) {
						label=new Label(11, j, DateUtil.simpdfyMdHms.format(merchantwithdrawdetail.getGmtOrder()), format);
						sheet1.addCell(label);
					}
					label=new Label(12, j, ObjectUtil.getOrderState(merchantwithdrawdetail.getOrderStatus()), format);
					sheet1.addCell(label);
					label=new Label(13, j, ObjectUtil.getProfitStatus(merchantwithdrawdetail.getProfitStatus()), format);
					sheet1.addCell(label);
					if (merchantwithdrawdetail.getGmtComplete()!=null) {
						label=new Label(14, j, DateUtil.simpdfyMdHms.format(merchantwithdrawdetail.getGmtComplete()), format);
						sheet1.addCell(label);
					}
					label=new Label(15, j, "易宝分账", format);
					sheet1.addCell(label);
					if (merchantwithdrawdetail.getGmtWithdraw()!=null) {
						label=new Label(16, j, DateUtil.simpdfyMdHms.format(merchantwithdrawdetail.getGmtWithdraw()), format);
						sheet1.addCell(label);
					}
				}
				workbook.write();
				workbook.close();
				InputStream inStream=new FileInputStream(fileUrl);
				byte[]	data = ObjectUtil.readInputStream(inStream);
				String dowmupload=OSSUtil.upload(data, ".xls");
				if (dowmupload!=null&&dowmupload.length()>0) {
					userorderexcelService.saveOrUpdate(new Userorderexcel(sysuser.getId(), 3, dowmupload));
				}				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}

	@Override
	public Merchantwithdrawdetail getForUpdate(Long id) {
		return merchantwithdrawdetailDao.getForUpdate(id);
	}

}